package com.yaorange.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.dao.EmpMapper;
import com.yaorange.entity.Emp;
import com.yaorange.service.EmpService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {

    @Resource
    private EmpMapper empMapper;

    //分页
    @Override
    public PageInfo<Emp> getPage(Integer pageNum, Integer pageSize, String conditionEmpName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Emp> emps = empMapper.selectByXml(conditionEmpName);
        return new PageInfo<>(emps);
    }

    @Override
    public int addEmp(Emp emp) {
        int insert = empMapper.insert(emp);
        return insert;
    }


    //修改会用
    @Override
    public Emp getEmp(Integer empId) {
        Emp emp = empMapper.selectByPrimaryKey(empId);
        return emp;
    }

    //真正修改操作
    @Override
    public int updateEmp(Emp emp) {
        int i = empMapper.updateByPrimaryKeySelective(emp);
        return i;
    }

    @Override
    public int update(Emp emp) {
        return empMapper.updateByPrimaryKeySelective(emp);
    }

    @Override
    public Emp getEmpByUserName(String username) {
        Example example = new Example(Emp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("empAccount",username);
        List<Emp> empList = empMapper.selectByExample(example);
        return empList.size() == 0 ? null : empList.get(0);
    }

    @Override
    public List<Emp> getEmpListByDeptId(Integer deptId) {
        Example example = new Example(Emp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("empState",0);//获取在职的员工
        criteria.andEqualTo("deptId",deptId);//指定部门下的员工
        return empMapper.selectByExample(example);
    }


}
